package com.fcat.base.common.security;

import com.fcat.base.common.util.ByteAndHexUtil;
import com.fcat.base.common.util.StrUtil;

import java.security.MessageDigest;

public class HashUtil {

	private static final String ALGORITHM_DEFAULT = "MD5";
	private static final int BUFFER_SIZE = 4096;

	private HashUtil() {
	}

	public static String hash(byte[] sByte) {
		return hash(sByte, ALGORITHM_DEFAULT);
	}

	/**
	 * SHA-256||SHA-512||SHA||SHA-384||MD5||MD2
	 * 
	 * @param sByte
	 * @param algorithm
	 * @return
	 * @throws Exception
	 */
	public static String hash(byte[] sByte, String algorithm) {
		if (sByte == null || sByte.length < 1) {
			return "";
		}
		if (StrUtil.isBlank(algorithm)) {
			algorithm = ALGORITHM_DEFAULT;
		}
		try {
			MessageDigest messagedigest = MessageDigest.getInstance(algorithm);
			messagedigest.update(sByte, 0, sByte.length);
			byte[] digest = messagedigest.digest();
			return ByteAndHexUtil.bytesToHexs(digest);

		} catch (Exception e) {

		}
		return "";
	}

	public static String md5(byte[] sByte) {
		return hash(sByte, "MD5");
	}

}